﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using HiStaff.Domain.HU;
using HiStaff.iSystem;
using System.Linq;
using System.Text;

namespace HiStaff.Control
{
    [ToolboxBitmap(typeof(TreeView))]
    public class HiComboOrganization : HiComboTreeBox
    {
        private static List<HU_ORGANIZATION> lstOrg;
        private bool _isRequire = false;
        private bool _readOnlyWhenEdit = false;

        public string DataField { get; set; }
        public decimal? OrganizationLevelID { get; set; }
        public decimal? OrganizationStatus { get; set; }
        public bool IsRequire
        {
            get
            {
                return _isRequire;
            }
            set
            {
                _isRequire = value;
                if (value)
                    this.BackColor = HiStaff.iSystem.Data.COLOR_REQUIREMENT;
                else this.BackColor = HiStaff.iSystem.Data.COLOR_NONREQUIREMENT;
            }
        }
        public bool ReadOnlyWhenEdit
        {
            get { return _readOnlyWhenEdit; }
            set { _readOnlyWhenEdit = value; }
        }
        public HiComboOrganization()
        {
            this.Height = 20;
            this.ShowPath = true;
            this.PathSeparator = " » ";
        }

        protected override void OnCreateControl()
        {
            try
            {

                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    ReLoad();
                }
            }
            catch
            {
                return;
            }
        }
        public void ReLoad()
        {
            var _lst = Service.ProfileClient.GetOrganization();

            lstOrg = _lst.FindAll(obj => (!OrganizationLevelID.HasValue ||  obj.ORGLEVELID <= OrganizationLevelID) &&
               (!OrganizationStatus.HasValue || obj.STATUS == OrganizationStatus));

            this.Nodes.Clear();
            
            if (lstOrg != null)
            {
                //Populate base nodes
                var query = (from c in lstOrg
                             where !(from o in lstOrg
                                     select o.ID)
                                    .Contains(c.PARENTID)
                             select c).ToList();
                foreach (var item in query)
                {
                    var trNode = new ComboTreeNode(item.NAME);
                    trNode.Name = item.ID.GetValueOrDefault(-1).ToString();
                    trNode.Tag = item.ORGLEVELID;
                    this.Nodes.Add(trNode);

                    PopulateTreeView(trNode);

                    trNode.Expanded = true;
                }
            }
        }
        protected void PopulateTreeView(ComboTreeNode trParentNode)
        {
            var query = (from c in lstOrg
                         where c.PARENTID.Equals(decimal.Parse(trParentNode.Name))
                         select c).ToList();

            foreach (var item in query)
            {
                var trChildNode = new ComboTreeNode(item.NAME);
                trChildNode.Name = item.ID.GetValueOrDefault(-1).ToString();
                trChildNode.Tag = item.ORGLEVELID;
                trParentNode.Nodes.Add(trChildNode);
                PopulateTreeView(trChildNode);
            }
        }
        private ComboTreeNode FindNodeByName(ComboTreeNode root, string name)
        {
            foreach (ComboTreeNode tn in root.Nodes)
            {
                if (tn.Name == name)
                    return tn;
                else
                {
                    ComboTreeNode result = FindNodeByName(tn, name);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }
        public ComboTreeNode FindNodeByName(string name)
        {
            foreach (ComboTreeNode tn in this.Nodes)
            {
                if (tn.Name == name)
                    return tn;
                else
                {
                    ComboTreeNode result = FindNodeByName(tn, name);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }
    }
}
